package myky.com.doctorapp.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.facebook.drawee.view.SimpleDraweeView
import com.github.nukc.stateview.Constant
import myky.com.doctorapp.R
import myky.com.doctorapp.api.Address
import myky.com.doctorapp.entity.RemoteListEntity
import myky.com.doctorapp.utils.UriUtils

/**
 * Description:
 * @author wuyangwuyang
 * Created by 2019/1/9 on 17:47
 */
class ExpandListAdapter(private val mContext:Context,private val hopitallist:ArrayList<RemoteListEntity.PayLoadBean.HospitalListBean>) :BaseExpandableListAdapter(){

    private val inflater: LayoutInflater = LayoutInflater.from(mContext)

    override fun getGroup(p0: Int): Any =hopitallist[p0]

    override fun isChildSelectable(p0: Int, p1: Int): Boolean =true

    override fun hasStableIds(): Boolean =true

    override fun getGroupView(p0: Int, isExpanded: Boolean, p2: View?, p3: ViewGroup?): View {

        var convertView = p2
        val holder: ExpandListAdapter.GroupHolder
        var listBean = hopitallist.get(p0)
        if (convertView == null) {
            holder = ExpandListAdapter.GroupHolder()
            convertView = inflater.inflate(R.layout.remote_hospital_list_item, p3, false)
            holder.title = convertView!!.findViewById(R.id.remote_doctor_list_item_tv1)
            holder.group_expand_state = convertView.findViewById(R.id.remote_doctor_list_item_iv)
            holder.department = convertView.findViewById(R.id.activity_long_range_item_tv2)
            convertView.tag = holder
        } else {
            holder = convertView.tag as ExpandListAdapter.GroupHolder
        }


        holder.title!!.text=listBean.hospitalname

        return convertView
    }

    override fun getChildrenCount(p0: Int): Int =hopitallist[p0].doctors.size

    override fun getChild(p0: Int, p1: Int): Any =hopitallist[p0].doctors[p1]

    override fun getGroupId(p0: Int): Long = p0.toLong()

    override fun getChildView(groupPosition: Int, p1: Int, p2: Boolean, convertView: View?, p4: ViewGroup?): View {

        var convertView = convertView
        val holder: ExpandListAdapter.ChildHolder
        val item = hopitallist[groupPosition].doctors[p1]
        if (convertView == null) {
            holder = ExpandListAdapter.ChildHolder()
            convertView = inflater.inflate(R.layout.remote_doctor_list_item, p4, false)
            holder.doctorname = convertView!!.findViewById(R.id.remote_doctor_list_item_name)
            holder.header = convertView.findViewById(R.id.remote_doctor_list_item_header)
            holder.doctor_level = convertView.findViewById(R.id.remote_doctor_list_item_level)
            holder.departmentname = convertView.findViewById(R.id.remote_doctor_list_item_ks)
            holder.hospitalname = convertView.findViewById(R.id.remote_doctor_list_item_hospital)
            holder.description = convertView.findViewById(R.id.remote_doctor_list_item_ms)
            convertView.tag = holder
        } else {
            holder = convertView.tag as ExpandListAdapter.ChildHolder
        }
        holder.doctorname!!.text=item.realname
        holder.departmentname!!.text=item.departmentname
        holder.hospitalname!!.text=item.hospitalname
        holder.doctor_level!!.text=item.doctoradministration
        holder.description!!.text=item.speciality
        holder.header!!.setImageURI(UriUtils.getUriInstance().getUri(Constant.ONLINEPIC, Address.IMAGE.plus(item.headPhoto)))
        val rl_consoult = convertView.findViewById<RelativeLayout>(R.id.remote_doctor_item_smallbtn_consoult)
        val rl_remote = convertView.findViewById<RelativeLayout>(R.id.remote_doctor_list_smallbtn_remote)
        val rl_bespeak = convertView.findViewById<RelativeLayout>(R.id.remote_doctor_list_item_smallbtn_bespeak)
        val tv_consoult = convertView.findViewById<TextView>(R.id.remote_doctor_list_smallbtn_consoult_tv)
        val tv_remote = convertView.findViewById<TextView>(R.id.remote_doctor_list_item_smallbtn_remote_tv)
        val tv_bespeak = convertView.findViewById<TextView>(R.id.remote_doctor_list_item_smallbtn_bespeak_tv)
        if (item.doctorpermitnew != null) {
            if (item.doctorpermitnew.contains("1")) {
                rl_consoult.setBackgroundResource(R.drawable.small_btn_consoult)
                rl_consoult.isClickable = true
                tv_consoult.setTextColor( mContext.resources.getColor(R.color.blue_60c6f1))
            } else {
                rl_consoult.setBackgroundResource(R.drawable.small_btn_gray)
                rl_consoult.isClickable = false
                tv_consoult.setTextColor(mContext.resources.getColor(R.color.gray_d4d4d4))
            }
            if (item.doctorpermitnew.contains("2")) {
                rl_remote.setBackgroundResource(R.drawable.small_btn_remote)
                rl_remote.isClickable = true
                tv_remote.setTextColor(mContext.resources.getColor(R.color.orange_ffa77a))
            } else {
                rl_remote.setBackgroundResource(R.drawable.small_btn_gray)
                rl_remote.isClickable = false
                tv_remote.setTextColor(mContext.resources.getColor(R.color.gray_d4d4d4))
            }
            if (item.doctorpermitnew.contains("3")) {
                rl_bespeak.setBackgroundResource(R.drawable.small_btn_bespeak)
                rl_bespeak.isClickable = true
                tv_bespeak.setTextColor( mContext.resources.getColor(R.color.green_47d995))
            } else {
                rl_bespeak.setBackgroundResource(R.drawable.small_btn_gray)
                rl_bespeak.isClickable = false
                tv_bespeak.setTextColor(mContext.resources.getColor(R.color.gray_d4d4d4))
            }
        }
        return convertView
    }

    override fun getChildId(p0: Int, p1: Int): Long = p1.toLong()

    override fun getGroupCount(): Int =hopitallist.size


    private class ChildHolder {
        internal var doctorname: TextView? = null
        internal var departmentname: TextView? = null
        internal var hospitalname: TextView? = null
        internal var doctor_level: TextView? = null
        internal var description: TextView? = null
        internal var header: SimpleDraweeView? = null
    }

    private class GroupHolder {
        internal var title: TextView? = null
        internal var department: TextView? = null
        internal var group_expand_state: ImageView? = null

    }
}